package com.zhanhong.opencvdemo.view;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.zhanhong.opencvdemo.R;
import com.zhanhong.opencvdemo.dialog.SimpleProgressDialog;
import com.zhanhong.opencvdemo.presenter.CropPresenter;
import com.zhanhong.opencvdemo.presenter.CropPresenterImpl;
import com.zhanhong.opencvdemo.value.IntentKey;

import me.pqpo.smartcropperlib.view.CropImageView;

public class CropActivity extends AppCompatActivity {

    private CropPresenter mPresenter;
    private CropImageView mIvCrop;
    private SimpleProgressDialog mProgressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_crop);
        initData();
        initView();
    }

    private void initData() {
        mPresenter = new CropPresenterImpl(this);
    }

    private void initView() {
        mIvCrop = findViewById(R.id.iv_crop);

        findViewById(R.id.tv_exit).setOnClickListener(v -> {
            setResult(RESULT_CANCELED);
            finish();
        });

        findViewById(R.id.tv_confirm).setOnClickListener(v -> {
            if (mIvCrop.canRightCrop()) {
                mPresenter.crop(mIvCrop);
            } else {
                showAlertDialog("答题卡框选错误");
            }
        });

        String srcFilePath = getIntent().getStringExtra(IntentKey.KEY_CROP_PATH);
        final Bitmap srcBitmap = BitmapFactory.decodeFile(srcFilePath);
        mPresenter.analyzeSrcBitmap(srcBitmap);
    }

    private void showProgress(String content) {
        if (mProgressDialog == null) {
            mProgressDialog = new SimpleProgressDialog(this);
        }
        mProgressDialog.show();
        mProgressDialog.setContent(content);
    }

    private void hideProgress() {
        if (mProgressDialog != null && mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
        }
    }

    public void onAnalyzeSrcBitmapProgress(String stepName, int current, int total, boolean success) {
        if (success) {
            if (current >= total) {
                hideProgress();
            } else {
                showProgress(stepName);
            }
        } else {
            hideProgress();
            showAlertDialog(stepName);
        }
    }

    public void onAnalyzeSrcBitmapComplete(Bitmap bitmap, Point[] points, boolean success) {
        hideProgress();
        mIvCrop.setImageToCrop(bitmap);
        mIvCrop.setCropPoints(points);
    }

    public void onCropComplete(String cropPath) {
        hideProgress();
        Intent intent = new Intent();
        intent.putExtra(IntentKey.KEY_CROP_PATH, cropPath);
        setResult(RESULT_OK, intent);
        finish();
    }

    public void onCropError(Exception e) {
        setResult(RESULT_CANCELED);
        showAlertDialog(e.getMessage());
    }

    private void showAlertDialog(String content) {
        AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setCancelable(false);
        alertDialog.setTitle("提示");
        alertDialog.setMessage(content);
        alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "我知道了", (dialog, which) -> {
            dialog.dismiss();
        });
        alertDialog.show();
    }
}
